﻿namespace AzureCamp.RssAggregatorBusiness
{
    using System;
    using System.Collections.Generic;
    using System.Data.Services.Common;
    using System.IO;
    using System.ServiceModel.Syndication;

    [DataServiceKey("PartitionKey", "RowKey")]
    public class Feed : Microsoft.Samples.ServiceHosting.StorageClient.TableStorageEntity
    {
        #region Fields
        private string title;
        private string owner;
        private string category;
        private string description;
        private List<RssLink> rssUrls;
        private int score;
        private int subscribers;
        private DateTime creationDate;
        private DateTime lastUpdateDate;
        #endregion

        #region Properties
        public DateTime CreationDate
        {
            get { return creationDate; }
            set { this.creationDate = value; }
        }

        public DateTime LastUpdateTime
        {
            get { return this.lastUpdateDate; }
            set { this.lastUpdateDate = value; }
        }

        public string Owner
        {
            get { return this.owner; }
            set { this.owner = value; }
        }

        public string Title
        {
            get { return this.title; }
            set { this.title = value; }
        }

        public string Category
        {
            get { return this.category; }
            set { this.category = value; }
        }

        public string Description
        {
            get { return this.description; }
            set { this.description = value; }
        }

        public int Score
        {
            get { return this.score; }
            set { this.score = value; }
        }

        public int Subscribers
        {
            get { return this.subscribers; }
            set { this.subscribers = value; }
        }
        #endregion 

        public Feed()
            : this("unspecified", "unknown")
        {
            Init();
        }

        public Feed(string t, string c)
        {
            this.Title = t;
            this.Category = c;
            Init();
        }

        private void Init()
        {
            this.PartitionKey = this.Title.ToLower();
            this.RowKey = RowKey = string.Format("{0:10}_{1}", DateTime.MaxValue.Ticks - DateTime.Now.Ticks, Guid.NewGuid());

            //CultureInfo ci = new CultureInfo("en-US");
            //System.Threading.Thread.CurrentThread.CurrentCulture = ci;
            this.lastUpdateDate = DateTime.UtcNow.AddHours(-2).ToUniversalTime();
            //this.lastUpdateDate = new DateTime(
            //    lastUpdateDate.Year,
            //    lastUpdateDate.Month,
            //    lastUpdateDate.Day,
            //    lastUpdateDate.Hour,
            //    lastUpdateDate.Minute,
            //    lastUpdateDate.Second,
            //    ci.Calendar);


            this.rssUrls = new List<RssLink>();

            this.creationDate = DateTime.UtcNow.ToUniversalTime();
            this.Score = 0;
            this.Subscribers = 0;
            this.Description = "no description";
        }

        public void AddRssLink(string url)
        {
            RssLink link = new RssLink(this.RowKey, url);
            this.rssUrls.Add(link);
        }

        public void RemoveRssLink(RssLink rssLink)
        {
            if (this.rssUrls.Contains(rssLink))
            {
                this.rssUrls.Remove(rssLink);
            }
        }

        public void ScoresUp()
        {
            this.Score++;
        }

        public void ScoresDown()
        {
            this.Score--;
        }

        public void SubscribersUp()
        {
            this.Subscribers++;
        }

        public void SubscribersDown()
        {
            this.Subscribers--;
        }

        public Stream GetAgregatorAsStream(string outputFile)
        {
            return GetAgregatorAsStream(outputFile, 20);
        }

        public Stream GetAgregatorAsStream(string outputFile, int numberOfPost)
        {
            List<string> links = new List<string>();
            foreach (RssLink r in this.rssUrls)
            {
                links.Add(r.Url);
            }
            SyndicationFeed feed = RssHelper.CreateAgregator(this.title, this.description, links, numberOfPost);
            RssHelper.SaveSyndicationFeedAsXml(feed, outputFile);

            FileStream stream = new FileStream(outputFile, FileMode.Open);
            return stream;
        }

        public Stream GetAgregatorAsStream(int numberOfPost)
        {
            List<string> links = new List<string>();
            foreach (RssLink r in this.rssUrls)
            {
                links.Add(r.Url);
            }
            SyndicationFeed feed = RssHelper.CreateAgregator(this.title, this.description, links, numberOfPost);
            MemoryStream stream = new MemoryStream();
            RssHelper.SaveSyndicationFeedAsXml(feed, stream);
            return stream;
        }

        public List<RssLink> GetUrls()
        {
            return this.rssUrls;
        }

        public void UpdateFeed()
        {
            this.LastUpdateTime = DateTime.Now;
        }

        internal void AddRssLink(RssLink r)
        {
            if(!this.rssUrls.Contains(r))
            this.rssUrls.Add(r);
        }
    }
}
